{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import argparse\n",
    "import pywt\n",
    "import random\n",
    "import pandas as pd\n",
    "\n",
    "def parse_args():\n",
    "    parser = argparse.ArgumentParser()\n",
    "    parser.add_argument('--input', type=str, \n",
    "                        default=\"/home/xiaoyifu/data/plant/rice_ribenqing/train/samples_CHH.hc_poses.signal_bilstm.denoise_fp8.tsv\",\n",
    "                        help='the input filepath')\n",
    "    parser.add_argument('--outputdir', type=str, \n",
    "                        default=\"/home/xiaoyifu/data/plant/rice_ribenqing/train/\",\n",
    "                        help='the picture output filepath')\n",
    "    return parser.parse_args([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_a_line(line):\n",
    "    words = line.strip().split()\n",
    "    base_means = np.array([float(x) for x in words[7].split(\",\")])\n",
    "    base_stds = np.array([float(x) for x in words[8].split(\",\")])\n",
    "    base_signal_lens = np.array([int(x) for x in words[9].split(\",\")])\n",
    "    k_signal = np.array([[float(y) for y in x.split(\",\")] for x in words[10].split(\";\")]).flatten()\n",
    "    signals_freq=np.fft.fft(k_signal)#np.array([[float(y) for y in x.split(\",\")] for x in words[13].split(\";\")])\n",
    "    magnitude=np.abs(signals_freq)\n",
    "    phase=np.angle(signals_freq)\n",
    "    # 进行小波变换\n",
    "    wavelet = 'db4'  # 小波基函数，这里选择 Daubechies 4\n",
    "    level = 5        # 分解级数\n",
    "    coeffs = pywt.wavedec(k_signal, wavelet, level=level)\n",
    "    # 设计高通滤波器\n",
    "    high_pass_filter = np.array([1, -1])  # 一阶差分滤波器\n",
    "\n",
    "    # 将高通滤波器应用于小波系数\n",
    "    filtered_coeffs = [np.convolve(c, high_pass_filter, mode='same') for c in coeffs]\n",
    "    signals_freq = pywt.waverec(filtered_coeffs, wavelet)\n",
    "\n",
    "    label = int(words[11])\n",
    "\n",
    "    return k_signal,base_means,base_stds,base_signal_lens,magnitude,phase,signals_freq ,label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_lines(input_path):\n",
    "    with open(input_path, \"r\") as f:\n",
    "        total_line = len(f.readlines())\n",
    "    return total_line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample_read(sample_num,input):\n",
    "    total_lines=get_lines(input)\n",
    "    sample_list=random.sample(range(total_lines),sample_num)\n",
    "    index=0\n",
    "    df = pd.DataFrame(columns=['signal', 'magnitude','phase','signals_freq','label'])\n",
    "    with open(input, \"r\") as f:\n",
    "        for line in f:\n",
    "            if index in sample_list:\n",
    "                signal,base_means,base_stds,base_signal_lens,magnitude,phase,signals_freq ,label=parse_a_line(line)\n",
    "                new_row = pd.DataFrame({'signal': signal, \n",
    "                           'magnitude':magnitude,'phase':phase,'signals_freq':signals_freq,'label':label})\n",
    "                df = pd.concat([df, new_row], ignore_index=True)\n",
    "            index+=1\n",
    "    return df\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "args=parse_args()\n",
    "df=sample_read(10000,args.input)\n",
    "correlation_matrix = df.corr()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>signal</th>\n",
       "      <th>magnitude</th>\n",
       "      <th>phase</th>\n",
       "      <th>signals_freq</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>signal</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.014278</td>\n",
       "      <td>-0.008962</td>\n",
       "      <td>0.669545</td>\n",
       "      <td>-0.079554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>magnitude</th>\n",
       "      <td>-0.014278</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.021812</td>\n",
       "      <td>-0.007037</td>\n",
       "      <td>0.026884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>phase</th>\n",
       "      <td>-0.008962</td>\n",
       "      <td>0.021812</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.002412</td>\n",
       "      <td>0.000458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>signals_freq</th>\n",
       "      <td>0.669545</td>\n",
       "      <td>-0.007037</td>\n",
       "      <td>0.002412</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.002975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>label</th>\n",
       "      <td>-0.079554</td>\n",
       "      <td>0.026884</td>\n",
       "      <td>0.000458</td>\n",
       "      <td>-0.002975</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                signal  magnitude     phase  signals_freq     label\n",
       "signal        1.000000  -0.014278 -0.008962      0.669545 -0.079554\n",
       "magnitude    -0.014278   1.000000  0.021812     -0.007037  0.026884\n",
       "phase        -0.008962   0.021812  1.000000      0.002412  0.000458\n",
       "signals_freq  0.669545  -0.007037  0.002412      1.000000 -0.002975\n",
       "label        -0.079554   0.026884  0.000458     -0.002975  1.000000"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "correlation_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def merge_except_last(row):\n",
    "    # 使用 np.concatenate() 将除最后一列以外的其他列合并到一起\n",
    "    merged_array = np.concatenate([row[:-1]])\n",
    "    return merged_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import ttest_ind\n",
    "\n",
    "feature_values_class0=[]\n",
    "feature_values_class1=[]\n",
    "for row in df.itertuples():\n",
    "    if row.label==0:\n",
    "        feature_values_class0.append(np.concatenate([row[:-2]]))\n",
    "    elif row.label==1:\n",
    "        feature_values_class1.append(np.concatenate([row[:-2]]))\n",
    "\n",
    "t_statistic, p_value = ttest_ind(feature_values_class0, feature_values_class1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-20.00008667 146.28839362 -49.29769975  -0.84000442]\n",
      "[5.56385256e-89 0.00000000e+00 0.00000000e+00 4.00905970e-01]\n"
     ]
    }
   ],
   "source": [
    "print(t_statistic)\n",
    "print(p_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import ttest_ind\n",
    "\n",
    "# 假设 df 是包含特征和分类目标的数据框\n",
    "feature_values_class1 = df[df['label'] == 0]['signal']\n",
    "feature_values_class2 = df[df['label'] == 1]['signal']\n",
    "\n",
    "t_statistic, p_value = ttest_ind(feature_values_class0, feature_values_class1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_values_class2=None"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "deepsignalpenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
